package com.pig4cloud.pigx.contract.api.v1.websocket.message;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.core.util.SpringContextHolder;
import org.springframework.web.socket.TextMessage;

/**
 * 开放式 api - webSocket 发送消息
 *
 */
public class OpenApiSendMessageV1 {

	public static TextMessage getTextMessage(R r) {
		try {
			ObjectMapper objectMapper = SpringContextHolder.getBean(ObjectMapper.class);
			String asString = objectMapper.writeValueAsString(r);
			return new TextMessage(asString);
		} catch (Exception e) {
			return new TextMessage((CharSequence) r);
		}
	}
}
